package cn.chia.back.dao.impl;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Component;

import cn.chia.back.dao.IOrderDAO;
import cn.chia.pojo.Orderitems;
import cn.chia.pojo.Orders;
import cn.chia.pojo.User;
import cn.chia.utils.Onuser;

@Component
public class OrderDAOImpl extends HibernateDaoSupport implements IOrderDAO {

	@Autowired
	public OrderDAOImpl(HibernateTemplate hibernateTemplate){
		super.setHibernateTemplate(hibernateTemplate);
	}

	public void doCreate(Orders vo) throws Exception {
		this.getHibernateTemplate().save(vo);
	}

	public void doRemove(Integer id) throws Exception {
		
	}
	//修改订单
	public void doUpdate(Orders vo) throws Exception {
		super.getHibernateTemplate().update(vo);
	}
	//查看所有订单
	public List<Orders> findAll() throws Exception {
		return super.getHibernateTemplate().find("from Orders order by id");
	}
	
	public List<Orders> findAll(int pageNo, int pageSize, String keyword,
			String column) throws Exception {
		DetachedCriteria c = DetachedCriteria.forClass(Orders.class); //Criteria完成分页查询。 
		c.add(Restrictions.like(column, "%" + keyword + "%"));
		List all = super.getHibernateTemplate().findByCriteria(c, (pageNo-1)*pageSize,pageSize);
		
		return all;
	}
	//根据id查订单
	public Orders findById(Integer id) throws Exception {
		return (Orders) super.getHibernateTemplate().find("from Orders where id='"+id+"' ").get(0);
	}
	
	//得到订单总条数
	public int getCount() throws Exception{
		return super.getHibernateTemplate().find("from Orders").size();
	}
	
	
	public int getAllCount(String keyword, String column) throws Exception {
		String hql = "SELECT COUNT(o) FROM Orders AS o WHERE o."+column+" LIKE ?";
		List all = super.getHibernateTemplate().find(hql, "%" + keyword +"%");
		return ((Long)all.get(0)).intValue();
	}
	//根据订单状态查询订单
	public List<?> findOrdersByState(String state) {
		return super.getHibernateTemplate().find("from Orders where state = '"+state+"'");
	}
	//李胜 通过Number查订单
	public Orders findByNumber(String number) {
		// TODO Auto-generated method stub
		System.out.println("Dao中orderNumber:"+number);
		return (Orders) this.getHibernateTemplate().find("from Orders where orderNumber='"+number+"'").get(0);
	}
	//李胜 增加订单明细
	public void addOrderItems(Orderitems vo) {
		// TODO Auto-generated method stub
		this.getHibernateTemplate().save(vo);
	}

	public List<?> findOverdueOrders() {
		return super.getHibernateTemplate().find("from Orders where flag = 2 and state = '已付款' and pinId.state = 1");
	}

	public List<?> findordersbystateuszdtime(String state, String uszdtime) {
		return super.getHibernateTemplate().find("from Orders where state='"+state+"' and uszdtime='"+uszdtime+"'");
	}
	
}
